import curses
from Putter.PutterColors import getColor

# Had some issues extending the window class, so 
# this is kind of more of a wrapper

#TODO Add addPreKeyEventHandler and addPostKeyEventHandler
class PutterWindow:
    def __init__(self, height, width, top, left, main):
        self.__height = height
        self.__width = width
        self.__top = top
        self.__left = left
        self.__main = main
        self.__isFocused = False

        self.__text = []

        self.__wrapper = curses.newwin(height, width, top, left) 
        self.__wrapper.box()
        self.__wrapper.bkgd(" ", getColor("wb") | curses.A_BOLD)

        self.__area = curses.newwin(height-2, width-2, top+1, left+1)
        self.__area.bkgd(" ", getColor("wb"))

    def refresh(self):
        linen = 0
        for line in self.__text:
            #TODO: Parse for color codes
            self.__area.addstr(linen,0,line)
            linen += 1

        self.__wrapper.refresh()
        self.__area.refresh()

    def addLine(self, text):
        self.__text.append(text)
        self.__main.redraw()

    def keyRead(self, ch):
        if ch == 9: # TAB
            self.focusNext()

    def setFocus(self, focus):
        self.__isFocused = focus
        if self.__isFocused:
            self.__wrapper.bkgd(" ", getColor("cb") | curses.A_BOLD)
            self.__main.focusedArea = self
        else:
            self.__wrapper.bkgd(" ", getColor("wb") | curses.A_BOLD)
        self.__main.redraw()

    def isFocused(self):
        return self.__isFocused

    def setNextFocus(self, window):
        self.nextFocus = window

    def focusNext(self):
        self.setFocus(False)
        self.nextFocus.setFocus(True)
